{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3cd172d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Created on Mon May 16 19:00:32 2022\n",
    "@author: Ajit Johnson Nirmal\n",
    "SCIMAP tutorial May 2022\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a1c1cc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load packages\n",
    "import scimap as sm\n",
    "import scanpy as sc\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2f30407",
   "metadata": {},
   "outputs": [],
   "source": [
    "common_path = \"/Users/aj/Dropbox (Partners HealthCare)/conferences/scimap_tutorial/may_2022_tutorial/\"\n",
    "#common_path = \"C:/Users/ajn16/Dropbox (Partners HealthCare)/conferences/scimap_tutorial/may_2022_tutorial/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd0fb93",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load data\n",
    "adata = sm.pp.mcmicro_to_scimap (image_path= str(common_path) + 'exemplar_001/quantification/unmicst-exemplar-001_cell.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbdfbc6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Markers in dataset\n",
    "adata.var.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6dbf7903",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "85dd9c27",
   "metadata": {},
   "source": [
    "### manually gate the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5635720e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# manually gate the data\n",
    "image_path = str(common_path) + 'exemplar_001/registration/exemplar-001.ome.tif'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08a0b002",
   "metadata": {},
   "outputs": [],
   "source": [
    "marker_of_interest = 'ECAD'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "515e6777",
   "metadata": {},
   "outputs": [],
   "source": [
    "sm.pl.gate_finder (image_path, adata, marker_of_interest, \n",
    "                   from_gate = 5, to_gate = 9, increment = 0.1, \n",
    "                   point_size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cb6e2fa",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "09979416",
   "metadata": {},
   "source": [
    "### rescale the data based on the manual gates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ac96e30",
   "metadata": {},
   "outputs": [],
   "source": [
    "manual_gate = pd.read_csv(str(common_path) + 'manual_gates.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "887f0e1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "manual_gate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5690359",
   "metadata": {},
   "outputs": [],
   "source": [
    "# rescale the data\n",
    "adata = sm.pp.rescale (adata, gate=manual_gate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "631d6bfa",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "945752d5",
   "metadata": {},
   "source": [
    "### Phenotyping cells"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8389e2a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the phenotyping workflow\n",
    "phenotype = pd.read_csv(str(common_path) + 'phenotype_workflow.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5fd0df31",
   "metadata": {},
   "outputs": [],
   "source": [
    "phenotype.style.format(na_rep='')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d79c3c5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run the phenotyping algorithm\n",
    "adata = sm.tl.phenotype_cells (adata, phenotype=phenotype, label=\"phenotype\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ea58d0b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Check the number of phenotyped cells\n",
    "adata.obs['phenotype'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d991678a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Visualize cell types\n",
    "sm.pl.image_viewer (image_path, adata, overlay = 'phenotype', point_color='white', point_size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ae6d36a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# add seg mask\n",
    "seg_mask_path = str(common_path) + 'exemplar_001/qc/s3seg/unmicst-exemplar-001/cellOutlines.ome.tif'\n",
    "sm.pl.image_viewer (image_path, adata, \n",
    "                    seg_mask = seg_mask_path,\n",
    "                    overlay = 'phenotype', \n",
    "                    point_color='white', \n",
    "                    point_size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "095664b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Visualize heatmap of cell types\n",
    "sc.pl.matrixplot(adata, var_names= adata.var.index, groupby='phenotype', dendrogram=True, use_raw=False, cmap=\"vlag\", standard_scale='var')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c522f9d",
   "metadata": {},
   "source": [
    "### Voronoi Plots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1890486b",
   "metadata": {},
   "outputs": [],
   "source": [
    "sm.pl.voronoi(adata, color_by='phenotype', \n",
    "                 voronoi_edge_color = 'black',\n",
    "                 voronoi_line_width = 0.3, \n",
    "                 voronoi_alpha = 0.8, \n",
    "                 size_max=5000,\n",
    "                 overlay_points=None, \n",
    "                 plot_legend=True, \n",
    "                 legend_size=6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d49197a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Map user defined colors\n",
    "colors = {'ASMA+ cells': '#8AC926', \n",
    "          'Myeloid': \"#E9D8A6\", \n",
    "          'NK cells':  \"#0A9396\",\n",
    "          'Neutrophils': \"#CA6702\", \n",
    "          'Other Immune cells':'#001219',\n",
    "          'Treg': \"#005F73\", \n",
    "          'Tumor':  \"#9B2226\",\n",
    "          'Unknown': '#BCB8B1'\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b19c466",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.rcParams['figure.figsize'] = [15, 10]\n",
    "sm.pl.voronoi(adata, color_by='phenotype', \n",
    "                  colors = colors,\n",
    "                 voronoi_edge_color = 'black',\n",
    "                 voronoi_line_width = 0.3, \n",
    "                 voronoi_alpha = 0.8, \n",
    "                 size_max=5000,\n",
    "                 overlay_points=None, \n",
    "                 plot_legend=True, \n",
    "                 legend_size=6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9729f34",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "29e2f5b2",
   "metadata": {},
   "source": [
    "### Adding ROI to images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9dac364",
   "metadata": {},
   "outputs": [],
   "source": [
    "adata = sm.pl.addROI_image(image_path, adata, \n",
    "                             subset=None, \n",
    "                             imageid='imageid', \n",
    "                             overlay=None, overlay_category=None,\n",
    "                             markers=None, \n",
    "                             channel_names='default', \n",
    "                             x_coordinate='X_centroid', y_coordinate='Y_centroid', \n",
    "                             seg_mask=None, \n",
    "                             overwrite=True, \n",
    "                             label='ROI')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8eed126c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# check ROI cell count\n",
    "adata.obs['ROI'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21955cb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add ROI individually\n",
    "adata = sm.pl.addROI_image(image_path, adata, \n",
    "                     overwrite=True, \n",
    "                     label='ROI_individual')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96e6bfb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# check number of cells\n",
    "adata.obs['ROI_individual'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b71d10a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Scatter plot to show the differnt ROI's\n",
    "import pandas as pd\n",
    "import plotly.express as px\n",
    "import plotly.io as pio\n",
    "pio.renderers.default = 'browser'\n",
    "def plotly (adata,phenotype,image_id=None,x='X_centroid',y='Y_centroid',size=2, **kwargs):\n",
    "    if image_id is not None:\n",
    "        adata = adata[adata.obs['imageid'] == image_id]    \n",
    "    data = pd.DataFrame({'x':adata.obs[x], 'y':adata.obs[y],'col': adata.obs[phenotype]})\n",
    "    data = data.sort_values(by=['col'])\n",
    "    fig = px.scatter(data, x=\"x\", y=\"y\", color=\"col\", **kwargs)\n",
    "    fig.update_traces(marker=dict(size=size),selector=dict(mode='markers'),hoverlabel = dict(namelength = -1))\n",
    "    fig.update_yaxes(autorange=\"reversed\", tickformat='g')\n",
    "    fig.update_xaxes(tickformat='g')\n",
    "    fig.update_layout({'plot_bgcolor': 'rgba(0, 0, 0, 0)','paper_bgcolor': 'rgba(0, 0, 0, 0)'})\n",
    "    return fig\n",
    "\n",
    "plotly (adata,phenotype='ROI_individual',image_id=None,x='X_centroid',y='Y_centroid',size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f2f6974",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# voronoi plot\n",
    "sm.pl.voronoi(adata, color_by='ROI_individual', \n",
    "                 voronoi_edge_color = 'black',\n",
    "                 voronoi_line_width = 0.3, \n",
    "                 voronoi_alpha = 0.8, \n",
    "                 size_max=5000,\n",
    "                 overlay_points=None, \n",
    "                 plot_legend=True, \n",
    "                 legend_size=6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88ea2d24",
   "metadata": {},
   "outputs": [],
   "source": [
    "# save adata\n",
    "adata.write(str(common_path) + 'may2022_tutorial.h5ad')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd08a1d2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
